/**
 * 
 */
package org.database.filter;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author tamnguyen
 * 
 */
@Configuration
@ImportResource(value = { "classpath:/META-INF/spring/*db-config.xml" })
@EnableTransactionManagement
public class AppConfig
{
	@Value("${jdbc.username}")
	private String _username;
	@Value("${jdbc.password}")
	private String _password;
	@Value("${jdbc.url}")
	private String _connectionURL;
	@Value("${jdbc.driver}")
	private String _driver;
	@Value("${hibernate.showsql}")
	private boolean _showSql;
	@Value("${hibernate.hbm2ddl.auto}")
	private String _hbm2ddl;
	@Value("${hibernate.dialect}")
	private String _dialect;
	
	

	@Bean
	public LocalContainerEntityManagerFactoryBean containerEntityManagerFactoryBean()
	{
		LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
		bean.setPackagesToScan(new String[] { "org.database.filter.model" });
		bean.setJpaPropertyMap(jpaMap());
		bean.setJpaVendorAdapter(jpaVendorAdapter());
		bean.setPersistenceUnitName("cxf");
		bean.setDataSource(dataSource());
		return bean;
	}

	private DataSource dataSource()
	{
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName(_driver);
		dataSource.setPassword(_password);
		dataSource.setUsername(_username);
		dataSource.setUrl(_connectionURL);
		return dataSource;
	}

	@Bean
	public JpaVendorAdapter jpaVendorAdapter()
	{
		HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
//		adapter.setDatabase(_dbPlatform);
		adapter.setShowSql(_showSql);
		return adapter;
	}

	private Map<String, Object> jpaMap()
	{
		Map<String, Object> jpaProperties = new HashMap<String, Object>();
		jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, _hbm2ddl);
		jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, _dialect);
		return jpaProperties;
	}

	@Bean
	public PlatformTransactionManager transactionManager()
	{
		JpaTransactionManager manager = new JpaTransactionManager();
		manager.setEntityManagerFactory(containerEntityManagerFactoryBean().getObject());
		return manager;
	}

	@Bean
	public PersistenceExceptionTranslationPostProcessor exceptionTranslation()
	{
		return new PersistenceExceptionTranslationPostProcessor();
	}

	@Bean
	public HibernateExceptionTranslator hibernateExceptionTranslator()
	{
		return new HibernateExceptionTranslator();
	}
	
	
}
